//
//  APTagModel.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/3/2.
//

import Foundation
import UIKit

public protocol APTagModelProtocol: AnyObject {
    var isSelected: Bool { get set }
    func tagNormalAttributedString() -> NSMutableAttributedString?
    func tagSelectedAttributedString() -> NSMutableAttributedString?

    func tagName() -> String?
    func tagId() -> String?
    func isAll() -> Bool
}

extension APTagModelProtocol {
    public func tagNormalAttributedString() -> NSMutableAttributedString? {
        return nil
    }
    
    public func tagSelectedAttributedString() -> NSMutableAttributedString? {
        return nil
    }
    
    public func tagName() -> String? {
        return nil
    }
    
    public func tagId() -> String? {
        return nil
    }
    
    public func isAll() -> Bool {
        return false
    }
}

open class APTagModel: NSObject, APTagModelProtocol {
    
    public var isSelected: Bool = false
    
    public var text: String = "" {
        didSet {
            if id == "0" {
                text = "全部"
            }
        }
    }
    public var count: Int = 0
    public var id: String = "" {
        didSet {
            if id == "0" {
                isSelected = true
            }
        }
    }
    
    public func tagNormalAttributedString() -> NSMutableAttributedString? {
        let attStr = NSMutableAttributedString()
        
        let sub1 = NSMutableAttributedString(string: "\(text)")
        sub1.addAttributes([.font: UIFont.systemFont(ofSize: 14)], range: NSRange(location: 0, length: sub1.length))
        sub1.addAttributes([.foregroundColor: UIColor.black], range: NSRange(location: 0, length: sub1.length))
        attStr.append(sub1)
        let sub2 = NSMutableAttributedString(string: "\(count)")
        sub2.addAttributes([.font: UIFont.boldSystemFont(ofSize: 18)], range: NSRange(location: 0, length: sub2.length))
        sub2.addAttributes([.foregroundColor: UIColor.blue], range: NSRange(location: 0, length: sub2.length))
        attStr.append(sub2)
        return attStr
    }

    public func tagId() -> String? {
        return id
    }
    
    public func tagName() -> String? {
        return text
    }
    
    public func isAll() -> Bool {
        return id == "0"
    }
}
